September 06, 2020
Displays data in a list
Single row of a table view
requires 2 delegate methods
두 번째에서는 각 index에서 디스플레이할 셀을 리턴 (UITableViewCell)
그래서 사용하는 method가 dequeueReusableCell
Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table.
Creating .xib file
create Cocoa Touch Class file under Views
folder
Register Customized Cell
ChatViewController.swift
의 viewDidLoad()
안에서
tableView.register(UINib(nibName: K.cellNibName, bundle: nil), forCellReuseIdentifier: K.cellIdentifier)
extend한 코드에서 as! MessageCell
추가
extension ChatViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath)
as! MessageCell
cell.label.text = messages[indexPath.row].body
return cell
}
}